package com.bonc.rms.service.impl.tms;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bonc.odk.logmanager.LogManager;
import com.bonc.rms.dao.tms.IEmployeeDetailsDao;
import com.bonc.rms.service.tms.IEmployeeDetailsService;
import com.bonc.rms.service.tms.ISalaryRecordService;
import com.bonc.rms.service.tms.ISalaryService;
import com.bonc.rms.utils.CalendarUtil;

@Service
public class EmployeeDetailsServiceImpl implements IEmployeeDetailsService{

	@Autowired
	IEmployeeDetailsDao employeeDetailsDao;
	 @Autowired
     ISalaryService salaryService;
	 @Autowired
	 ISalaryRecordService salaryRecordService;
	
	
	@Override
	public List<Map<String, Object>> getEmployeeDetails(String jobNumber) {
		LogManager.Method("In  Service : EmployeeDetailsServiceImpl -> getEmployeeDetails");
		LogManager.Method("Out Service : EmployeeDetailsServiceImpl -> getEmployeeDetails");
		return employeeDetailsDao.getEmployeeDetails(jobNumber);
	}
	@Override
	public List<Map<String, Object>> getEmployeeWagesDetails(String jobNumber) {
		LogManager.Method("In  Service : EmployeeDetailsServiceImpl -> getEmployeeWagesDetails");
		LogManager.Method("Out Service : EmployeeDetailsServiceImpl -> getEmployeeWagesDetails");
		return employeeDetailsDao.getEmployeeWagesDetails(jobNumber);
	}

	@Override
	public List<Map<String, Object>> getWagesDetails(String jobNumber) {
		LogManager.Method("In  Service : EmployeeDetailsServiceImpl -> getWagesDetails");
		LogManager.Method("Out Service : EmployeeDetailsServiceImpl -> getWagesDetails");
		return employeeDetailsDao.getwagesDetails(jobNumber);
	}
	
	@Override
	public List<Object> getPayrollCost(String jobNumber, String startDate, String endDate) {
		LogManager.Method("In  Service : EmployeeDetailsServiceImpl -> getPayrollCost");
        List<Object> retSalaryList = new ArrayList<Object>();
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date startMonthDate = null;
        Date endMonthDate = null;
        java.sql.Date startMonthDateSql = null;
        java.sql.Date endMonthDateSql = null;
        try {
            startMonthDate = sdf.parse(startDate);
            endMonthDate = sdf.parse(endDate);
            startMonthDateSql = new java.sql.Date(startMonthDate.getTime());
            endMonthDateSql = new java.sql.Date(endMonthDate.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        
        Calendar startDateCal = CalendarUtil.convertToCal(startDate);
        Calendar endDateCal = CalendarUtil.convertToCal(endDate);
        
        startDateCal.set(Calendar.DAY_OF_MONTH, 1);
        endDateCal.set(Calendar.DAY_OF_MONTH, endDateCal.getActualMaximum(Calendar.DAY_OF_MONTH));
        
        List<Map<String, Object>> salaryRecordList = 
                salaryRecordService.getSalaryRecordByJobNumberAndBetweenDate(
                        jobNumber, 
                        startMonthDateSql, 
                        endMonthDateSql
                    );
        
        List<Map<String, Object>> salaryRecordBeforeList = 
                salaryRecordService.getSalaryRecordByJobNumberAndBeforeDate(
                        (String) jobNumber, 
                        startMonthDateSql
                    );
        
        double adjustedBasicWage = 0.0;
        double adjustedPerformance = 0.0;
        double adjustedBonus = 0.0;
        double salary = 0.0;
        
        DecimalFormat df =(DecimalFormat) NumberFormat.getInstance();
        df.setMaximumFractionDigits(2);
        
        // 计算第一段时间内的 salary humanResourceCost
        if(salaryRecordBeforeList.size() > 0) {
            Map<String, Object> lastSalaryRecordBefore = salaryRecordBeforeList.get(salaryRecordBeforeList.size() - 1);
            adjustedBasicWage = ((BigDecimal) lastSalaryRecordBefore.get("adjusted_basic_wage")).doubleValue();
            adjustedPerformance = ((BigDecimal)  lastSalaryRecordBefore.get("adjusted_performance")).doubleValue();
            adjustedBonus = ((BigDecimal) lastSalaryRecordBefore.get("adjusted_bonus")).doubleValue();
            salary = adjustedBasicWage + adjustedPerformance + adjustedBonus;
        } else {
            List<Map<String, Object>> salaryList = salaryService.getSalaryByJobNumber((String) jobNumber);
            if(salaryList.size() > 0) {
                Map<String, Object> initSalary = salaryList.get(0);
                adjustedBasicWage = ((BigDecimal) initSalary.get("basic_wage")).doubleValue();
                adjustedPerformance = ((BigDecimal)  initSalary.get("performance")).doubleValue();
                adjustedBonus = ((BigDecimal)  initSalary.get("bonus")).doubleValue();
                salary = adjustedBasicWage + adjustedPerformance + adjustedBonus;
            }
        }
        
        if(salaryRecordList.size() > 0) {
            // 发生调薪

            Map<String, Object> map = null;
            Calendar calTemp = Calendar.getInstance();
            calTemp.setTime(startMonthDate);
            for(int i = 0 ; i < salaryRecordList.size(); i ++) {
                // 统计前几段时间内的工资
                map = salaryRecordList.get(i);
                java.sql.Timestamp adjustDate = (java.sql.Timestamp) map.get("adjust_date");
                Calendar adjustDateCal = Calendar.getInstance();
                adjustDateCal.setTime(adjustDate);
                
                for(int j = 0; j < CalendarUtil.calMonthDif(calTemp, adjustDateCal); j ++) {
            		retSalaryList.add(salary);
            	}
                
                calTemp.setTime(adjustDate);
                adjustedBasicWage = ((BigDecimal) map.get("adjusted_basic_wage")).doubleValue();
                adjustedPerformance = ((BigDecimal) map.get("adjusted_performance")).doubleValue();
                adjustedBonus = ((BigDecimal) map.get("adjusted_bonus")).doubleValue();
                salary = adjustedBasicWage + adjustedPerformance + adjustedBonus;
            }
                
            // 统计最后一段时间内的salary humanResourceCost
            map = salaryRecordList.get(salaryRecordList.size() - 1);
            java.sql.Timestamp adjustDate = (java.sql.Timestamp) map.get("adjust_date");
            calTemp.setTime(adjustDate);
            for(int j = 0; j < CalendarUtil.calMonthDif(calTemp, endDateCal) + 1; j ++) {
        		retSalaryList.add(salary);
        	}
            
        } else {
            // 未发生调薪
        	for(int i = 0; i < CalendarUtil.calMonthDif(startDateCal, endDateCal); i ++) {
        		retSalaryList.add(salary);
        	}
        }
        LogManager.Method("Out Service : EmployeeDetailsServiceImpl -> getPayrollCost");
		return retSalaryList;
	}

	@Override
	public List<Object> getSocialSecurityWelfareCost(String jobNumber, String startDate, String endDate) {
		LogManager.Method("In  Service : EmployeeDetailsServiceImpl -> getSocialSecurityWelfareCost");
		List<Object> retSalaryList = new ArrayList<Object>();
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date startMonthDate = null;
        Date endMonthDate = null;
        java.sql.Date startMonthDateSql = null;
        java.sql.Date endMonthDateSql = null;
        try {
            startMonthDate = sdf.parse(startDate);
            endMonthDate = sdf.parse(endDate);
            startMonthDateSql = new java.sql.Date(startMonthDate.getTime());
            endMonthDateSql = new java.sql.Date(endMonthDate.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        
        Calendar startDateCal = CalendarUtil.convertToCal(startDate);
        Calendar endDateCal = CalendarUtil.convertToCal(endDate);
        
        startDateCal.set(Calendar.DAY_OF_MONTH, 1);
        endDateCal.set(Calendar.DAY_OF_MONTH, endDateCal.getActualMaximum(Calendar.DAY_OF_MONTH));
        
        List<Map<String, Object>> salaryRecordList = 
                salaryRecordService.getSalaryRecordByJobNumberAndBetweenDate(
                        jobNumber, 
                        startMonthDateSql, 
                        endMonthDateSql
                    );
        
        List<Map<String, Object>> salaryRecordBeforeList = 
                salaryRecordService.getSalaryRecordByJobNumberAndBeforeDate(
                        (String) jobNumber, 
                        startMonthDateSql
                    );
        
        double adjustedSocialSecuritySelfareCost = 0.0;
        
        DecimalFormat df =(DecimalFormat) NumberFormat.getInstance();
        df.setMaximumFractionDigits(2);
        
        if(salaryRecordBeforeList.size() > 0) {
            Map<String, Object> lastSalaryRecordBefore = salaryRecordBeforeList.get(salaryRecordBeforeList.size() - 1);
            adjustedSocialSecuritySelfareCost = ((BigDecimal) lastSalaryRecordBefore.get("adjusted_social_security_welfare_cost")).doubleValue();
        } else {
            List<Map<String, Object>> salaryList = salaryService.getSalaryByJobNumber((String) jobNumber);
            if(salaryList.size() > 0) {
                Map<String, Object> initSalary = salaryList.get(0);
                adjustedSocialSecuritySelfareCost = ((BigDecimal)  initSalary.get("social_security_welfare_cost")).doubleValue();
            }
        }
        
        if(salaryRecordList.size() > 0) {
            // 发生调薪

            Map<String, Object> map = null;
            Calendar calTemp = Calendar.getInstance();
            calTemp.setTime(startMonthDate);
            for(int i = 0 ; i < salaryRecordList.size(); i ++) {
                // 统计前几段时间内的工资
                map = salaryRecordList.get(i);
                java.sql.Timestamp adjustDate = (java.sql.Timestamp) map.get("adjust_date");
                Calendar adjustDateCal = Calendar.getInstance();
                adjustDateCal.setTime(adjustDate);
                
                for(int j = 0; j < CalendarUtil.calMonthDif(calTemp, adjustDateCal); j ++) {
            		retSalaryList.add(adjustedSocialSecuritySelfareCost);
            	}
                
                calTemp.setTime(adjustDate);
                adjustedSocialSecuritySelfareCost = ((BigDecimal) map.get("adjusted_social_security_welfare_cost")).doubleValue();
            }
                
            // 统计最后一段时间内的salary humanResourceCost
            map = salaryRecordList.get(salaryRecordList.size() - 1);
            java.sql.Timestamp adjustDate = (java.sql.Timestamp) map.get("adjust_date");
            calTemp.setTime(adjustDate);
            for(int j = 0; j < CalendarUtil.calMonthDif(calTemp, endDateCal) + 1; j ++) {
        		retSalaryList.add(adjustedSocialSecuritySelfareCost);
        	}
            
        } else {
            // 未发生调薪
        	for(int i = 0; i < CalendarUtil.calMonthDif(startDateCal, endDateCal); i ++) {
        		retSalaryList.add(adjustedSocialSecuritySelfareCost);
        	}
        }
        LogManager.Method("Out Service : EmployeeDetailsServiceImpl -> getSocialSecurityWelfareCost");
		return retSalaryList;
	}
	
}
